package com.china_goldcard.pageindicatorview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 页面指示视图
 */
public class PageIndicatorView extends LinearLayout {

    private Context mContext;
    private float dotSize; // 指示器的大小（dp）
    private float dotMargin; // 指示器间距（dp）
    private int selectedColor;
    private int unSelectedColor;
    private List<View> indicatorViews; // 存放指示器

    public PageIndicatorView(Context context) {
        this(context, null);
    }

    public PageIndicatorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PageIndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        getConfig(context,attrs);
    }

    /**
     * 从xml中获取配置信息
     */
    private void getConfig(Context context, AttributeSet attrs) {
        //TypedArray是一个数组容器用于存放属性值
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.PageIndicatorView);
        dotSize = attributes.getDimension(R.styleable.PageIndicatorView_dotSize,dip2px(context,dotSize));
        dotMargin = attributes.getDimension(R.styleable.PageIndicatorView_dotMargin,dip2px(context,dotMargin));
        selectedColor = attributes.getResourceId(R.styleable.PageIndicatorView_selectedColor,R.drawable.dot_selected);
        unSelectedColor = attributes.getResourceId(R.styleable.PageIndicatorView_unSelectedColor,R.drawable.dot_normal);
        //用完务必回收容器
        attributes.recycle();
    }

    /**
     * 初始化指示器，默认选中第一页
     * @param count 指示器数量，即页数
     */
    public void initData(int count) {
        if (indicatorViews == null) {
            indicatorViews = new ArrayList<>();
        } else {
            indicatorViews.clear();
            removeAllViews();
        }
        for (int i = 0; i < count; i++) {
            addDot();
        }
        if (indicatorViews.size() == 1){
            setVisibility(GONE);
        }else{
            setVisibility(VISIBLE);
        }
        if (indicatorViews.size() > 0) {
           selectAtIndex(0);
        }
    }

    /**
     * 添加点
     */
    public void addDot(){
        LayoutParams params = new LayoutParams((int)dotSize, (int)dotSize);
        params.setMargins((int)dotMargin, (int)dotMargin,(int) dotMargin,(int) dotMargin);
        View view = new View(mContext);
        view.setBackgroundResource(unSelectedColor);
        addView(view, params);
        if (indicatorViews == null) {
            indicatorViews = new ArrayList<>();
        }
        indicatorViews.add(view);
    }



    /**
     * 设置选中页
     *
     * @param index 页下标，从0开始
     */
    public void selectAtIndex(int index) {
        for (int i = 0; i < indicatorViews.size(); i++) {
            if (i == index) {
                indicatorViews.get(i).setBackgroundResource(selectedColor);
            } else {
                indicatorViews.get(i).setBackgroundResource(unSelectedColor);
            }
        }
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     * @param context
     * @param dpValue 要转换的dp值
     */
    private int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     * @param context
     * @param pxValue 要转换的px值
     */
    private int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public int getUnSelectedColor() {
        return unSelectedColor;
    }

    public void setUnSelectedColor(int unSelectedColor) {
        this.unSelectedColor = unSelectedColor;
    }

    public int getSelectedColor() {
        return selectedColor;
    }

    public void setSelectedColor(int selectedColor) {
        this.selectedColor = selectedColor;
    }
}
